﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//using Window = AntdUI.Window;
using AntdUI;
using EONFULLSIN.Languages;
using EONFULLSIN.Models.Entity.User;
using EONFULLSIN.Utils;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;
using System.Reflection.Emit;
using AnalogInput;
using System.Collections.Concurrent;
//using Button = AntdUI.Button;
using Advantech.Adam;

namespace EONFULLSIN.Views.Manual
{
    public partial class EonfullManual : UserControl
    {

        private Window window;

        public EonfullManual()
        {
            InitializeComponent();
            //btnCh0.Click += btnChannel_Click;
            //btnCh1.Click += btnChannel_Click;
            //btnCh2.Click += btnChannel_Click;
            //btnCh3.Click += btnChannel_Click;
            //btnCh4.Click += btnChannel_Click;
            //btnCh5.Click += btnChannel_Click;
            //btnCh6.Click += btnChannel_Click;
            //btnCh7.Click += btnChannel_Click;
            InitAdam(this.panel_adamA,"ADAM-A", "COM1", AdamTaskQueueHelper.SharedQueueIns_A);
            InitAdam(this.panel_adamB, "ADAM-B", "COM2", AdamTaskQueueHelper.SharedQueueIns_B);
            LanguageChangedEvent.LanguageChanged += UpdateLanguage;
            ModbusTcpHelperZlan.AnalogEvent += DisplayLog;
            ModbusTcpHelperJy.AnalogEvent += DisplayLog;
            //AdamHelper.AnalogEvent += Adam_AnalogEvent;
            UpdateLanguage(LanguageHelper.CurrentLanguage);
        }

        private void DisplayLog(ConcurrentDictionary<int, float> obj)
        {
            txtJyLog.BeginInvoke(new Action(() =>
            {
                for (int i = 0; i < obj.Count; i++)
                {
                    txtJyLog.Text = "通道：" + i + "，" + "值：" + obj[i].ToString();
                }
            }));


        }

        private void DisplayLog(string part, ConcurrentDictionary<int, float> obj)
        {
            if (part == "dynamometer1")
            {
                txtLogZlan.BeginInvoke(new Action(() =>
                {
                    for (int i = 0; i < obj.Count; i++)
                    {
                        txtLogZlan.Text = "通道：" + i + "，" + "值：" + obj[i];
                    }
                }));
            }
        }

        private void UpdateLanguage(string obj)
        {

        }

        private void InitAdam(AntdUI.Panel p, string channel,string comport, Queue<ConcurrentDictionary<int, bool>> queueins)
        {
            //panel_adamA.Controls.Clear();
            //Control control = new AdamSubControl("COM1", AdamTaskQueueHelper.SharedQueueIns_A);
            //control.Dock = DockStyle.Fill;
            //panel_adamA.Controls.Add(control);
            p.Controls.Clear();
            Control control = new AdamSubControl(channel, comport, queueins);
            control.Dock = DockStyle.Fill;
            p.Controls.Add(control);
        }

        private void btnConnectZlan_Click(object sender, EventArgs e)
        {
            //zlanModbus.Ready(4096, 22, 1);
            //btnConnectZlan.Enabled = false;
            //btnDisconnectZlan.Enabled = true;
        }

        private void Zlan_AnalogEvent(ConcurrentDictionary<int, float> obj)
        {
            txtLogZlan.BeginInvoke(new Action(() =>
            {
                for (int i = 0; i < obj.Count; i++)
                {
                    txtLogZlan.Text = "通道：" + i + "，" + "值：" + obj[i];
                }
            }));

        }

        private void Jy_AnalogEvent(ConcurrentDictionary<int, float> obj)
        {
            txtJyLog.BeginInvoke(new Action(() =>
            {
                for (int i = 0; i < obj.Count; i++)
                {
                    txtJyLog.Text = "通道：" + i + "，" + "值：" + (obj[i] * 0.001).ToString();
                }
            }));

        }

        //private void Adam_AnalogEvent(ConcurrentDictionary<string, bool> obj)
        //{
        //    this.BeginInvoke(new Action(async () =>
        //    {

        //        foreach (var item in obj)
        //        {
        //            if (item.Key.Contains("COM1"))
        //            {
        //                string channel = item.Key.Substring(5, 1);
        //                //txtAdamLog.BeginInvoke(new Action(() =>
        //                //{
        //                //    txtAdamLog.AppendText($"{item.Key}:{item.Value}\r\n");
        //                //}));
        //                switch (channel)
        //                {
        //                    case "0":
        //                        txtCh0.Text = item.Value.ToString();
        //                        break;
        //                    case "1":
        //                        txtCh1.Text = item.Value.ToString();
        //                        break;
        //                    case "2":
        //                        txtCh2.Text = item.Value.ToString();
        //                        break;
        //                    case "3":
        //                        txtCh3.Text = item.Value.ToString();
        //                        break;
        //                    case "4":
        //                        txtCh4.Text = item.Value.ToString();
        //                        break;
        //                    case "5":
        //                        txtCh5.Text = item.Value.ToString();
        //                        break;
        //                    case "6":
        //                        txtCh6.Text = item.Value.ToString();
        //                        break;
        //                    case "7":
        //                        txtCh7.Text = item.Value.ToString();
        //                        break;
        //                }
        //                //await Task.Delay(1000);
        //            }
        //        }
        //    }));
        //}



        //private void btnChannel_Click(object sender, EventArgs e)
        //{
        //    string tempName = ((System.Windows.Forms.Button)sender).Name.ToString();
        //    if (tempName.Length == 6)
        //        tempName = tempName.Substring(5, 1);
        //    else
        //        tempName = tempName.Substring(5, 2);

        //    /*AdamHelper.m_triggerQueue*/
        //    ConcurrentDictionary<int, bool> pairs = new ConcurrentDictionary<int, bool>();
        //    TextBox txtBox = (TextBox)(this.Controls.Find("txtCh" + tempName, true)[0]);
        //    bool res = txtBox.Text == "False";
        //    pairs.TryAdd(Convert.ToInt32(tempName), res);
        //    AdamTaskQueueHelper.SharedQueueIns_A.Enqueue(pairs);
        //    //btn_Click(Convert.ToInt32(tempName), (TextBox)(this.Controls.Find("txtCh" + tempName, true)[0]));
        //}

    }
}
